package com.buyhouse.zhaimao.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.buyhouse.zhaimao.BaseFragment;
import com.buyhouse.zhaimao.R;
import com.buyhouse.zhaimao.model.HouseResouseBean;
import com.buyhouse.zhaimao.view.CircleImageView;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

public class ExpertMainFragment extends BaseFragment{
//列表
private ListView listview_houseResous;
//微信和电话
private LinearLayout linearLayout_char,linearLayout_mobile;
//内容  好评  等级  名字
private TextView info,goodCount,level,name;
//头像
private CircleImageView circleImageView;
//收藏
private Button favorite;
//适配器
private ExpertMainAdapter mAdapter;
//list
private List<HouseResouseBean> list = new ArrayList<HouseResouseBean>();


private ImageLoader imageLoader = ImageLoader.getInstance();
private DisplayImageOptions options, headOptions;

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected int getContentView() {
		return R.layout.fragment_expertmain;
	}

	@Override
	protected void initFragment() {
		initData();
		initView();
	}
	
	protected void initView() {
		/**加UI*/
		//微信和电话
		linearLayout_char = (LinearLayout) rootView.findViewById(R.id.linearLayout_char);
		linearLayout_mobile = (LinearLayout) rootView.findViewById(R.id.linearLayout_mobile);
		//头像
		circleImageView = (CircleImageView) rootView.findViewById(R.id.circleImageView);
		//内容  好评  等级  名字
		info= (TextView) rootView.findViewById(R.id.info);
		goodCount= (TextView) rootView.findViewById(R.id.goodCount);
		level= (TextView) rootView.findViewById(R.id.level);
		name= (TextView) rootView.findViewById(R.id.name);
		//收藏
		favorite= (Button) rootView.findViewById(R.id.favorite);
		//列表
		listview_houseResous = (ListView) rootView.findViewById(R.id.listview_houseResous);
		//适配器
		mAdapter = new ExpertMainAdapter();
		/**注册点击*/
		favorite.setOnClickListener(this);
		linearLayout_char.setOnClickListener(this);
		linearLayout_mobile.setOnClickListener(this);
		listview_houseResous.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				
			}
		});
		listview_houseResous.setAdapter(mAdapter);
	}
	
	protected void initData() {
		for(int i = 0;i<=5;i++){
		HouseResouseBean bean= new HouseResouseBean();
		bean.setAcreage("450");
		bean.setBedroom("2");
		bean.setCommission("4800");
		bean.setHall("1");
		bean.setPrice("256");
		bean.setTitle("标题标题");
		bean.setToilet("3");
		list.add(bean);
		}
	}
	
	class ExpertMainAdapter extends BaseAdapter{

		LayoutInflater inflater;
		
		public ExpertMainAdapter() {
			inflater = LayoutInflater.from(getActivity());
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder vh = null;
			if(null == convertView){
				convertView = inflater.inflate(R.layout.item_houseresouse, parent, false);
				
				vh = new ViewHolder();
				
				vh.imgUrl = (ImageView)convertView.findViewById(R.id.imgUrl);
				vh.title = (TextView)convertView.findViewById(R.id.title);
				vh.price = (TextView)convertView.findViewById(R.id.price);
				vh.bedroom_hall_toilet = (TextView)convertView.findViewById(R.id.bedroom_hall_toilet);
				vh.acreage = (TextView)convertView.findViewById(R.id.acreage);
				vh.commission = (TextView)convertView.findViewById(R.id.commission);
				
				//把ViewHolder当作“标记”保存到当前的选项视图实例中
				convertView.setTag(vh);
			}else{
				vh = (ViewHolder)convertView.getTag();
			}
			
			HouseResouseBean bean = list.get(position);
			vh.title.setText(bean.getTitle());
			vh.price.setText(bean.getPrice());
			vh.title.setText(bean.getBedroom()+"室"+bean.getHall()+"厅"+bean.getToilet()+"卫");
			vh.acreage.setText(bean.getAcreage());
			vh.commission.setText(bean.getCommission()+"元佣金");
			
			return convertView;
		}
		
	}
	private class ViewHolder{
		ImageView imgUrl;
		TextView title,price,bedroom_hall_toilet,acreage,commission;
		Button favorite;
	}
	private void initImgLoader(Context context) {
		imageLoader
				.init(new ImageLoaderConfiguration.Builder(context)
						.threadPriority(Thread.NORM_PRIORITY - 2)
						.threadPoolSize(3)
						.denyCacheImageMultipleSizesInMemory()
						.memoryCache(new WeakMemoryCache())
						.diskCacheFileNameGenerator(new Md5FileNameGenerator())
						.tasksProcessingOrder(QueueProcessingType.LIFO)
						.denyCacheImageMultipleSizesInMemory().writeDebugLogs()
						.build());
		options = new DisplayImageOptions.Builder()
				.cacheInMemory(true)
				// 设置下载的图片是否缓存在内存中
				.imageScaleType(ImageScaleType.EXACTLY)
				// 图像将完全按比例缩小的目标大小
				.cacheOnDisk(true)
				// 设置下载的图片是否缓存在SD卡中
				.showImageForEmptyUri(R.drawable.ic_home)
				.showImageOnFail(R.drawable.ic_home)
				.showImageOnLoading(R.drawable.ic_home)
				.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
				.bitmapConfig(Bitmap.Config.RGB_565).build();
		


	}
}
